package com.haixin.security.web;

import com.haixin.security.bean.Permission;
import com.haixin.security.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/permissions")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @PostMapping
    public ResponseEntity<String> addPermission(@RequestBody Permission permission) {
        permissionService.addPermission(permission);
        return ResponseEntity.ok("权限添加成功");
    }

    @PutMapping("/{id}")
    public ResponseEntity<String> updatePermission(@PathVariable int id, @RequestBody Permission permission) {
        permission.setId(id);
        permissionService.updatePermission(permission);
        return ResponseEntity.ok("权限更新成功");
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> deletePermission(@PathVariable int id) {
        permissionService.deletePermission(id);
        return ResponseEntity.ok("权限删除成功");
    }

    @GetMapping("/{id}")
    public ResponseEntity<Permission> getPermissionById(@PathVariable int id) {
        Permission permission = permissionService.getPermissionById(id);
        return ResponseEntity.ok(permission);
    }

    @GetMapping
    public ResponseEntity<List<Permission>> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        return ResponseEntity.ok(permissions);
    }
}

